<template>
    <div>
        <i-table size="default"   border stripe :columns="format" :data="show_list" :height="sharedState.tableHeight" >
            <template slot-scope="{ row, index }" slot="action">
                <Button  size="default"  type="error"   style="margin-bottom: 5px "  @click="del_key(index)">删除</Button>
            </template>
        </i-table >
        <div style="text-align: center;margin-top: 10px;">
            <Page ref="page" :total="total" :page-size="pageSize"  @on-change="changePage" show-total/>
        </div>
    </div>
</template>

<script>

import { post } from '@/api/http'
import store from '@/store'
import { bucket_key_list,delete_key } from  '@/api'


export default {
    data() {
        return {
            sharedState: store.state,
            format: [
                {
                    title: '名字',key: 'name',
                },
                {
                    title: 'bucket',key: 'bucket',
                },
                // {
                //     title: '原始文件名',key: 'filename',
                // },
                {
                    title: 'content_type',key: 'content_type',
                },
                // {
                //     title: '过期时间',key: 'expiry_date',
                // },
                {
                    title: '文件大小(单位M)',key: 'size',
                },
                {
                    title: '上次修改时间',key: 'last_modified',
                },
                {
                    title: '操作',slot: 'action',width: 200,align: 'center'
                }
            ],
            total_list: [],
            show_list: [],
            // 分页
            total: 0,
            pageSize: 15,
        }
    },
    methods: {
        changePage(index) {
            let _start = (index -1) * this.pageSize
            let _end = index * this.pageSize
            this.show_list = this.total_list.slice(_start,_end)
        },
        del_key(index){
            let name = this.show_list[index].name
            let result = confirm("确定要删除"+name+"吗?")
            if(result == false) return 
            let bucket = this.show_list[index].bucket

            let data = {"bucket_name": bucket,"name":name}
            post(delete_key,data,undefined,3000).then( (response) => {
                let info = JSON.stringify(response.data)
                if(info.indexOf('ok') != -1) {
                    this.$Message.success('删除成功')
                    this.refresh()
                }else {
                    alert(info)
                }
            })
        },
    },

    mounted: function() {
        let query = this.$router.currentRoute.query
        let name = query.name

        // let namespace = query.namespace
        console.log("获取到的参数name:",name)
        if (name == null  ){
            this.$Message.error("bucket name不能为空")
            return
        }
        let data = {'bucket_name':name}
        post(bucket_key_list,data,undefined,3000).then( (response) => {
            if(response.status==200){
                this.total_list = response.data
                this.total = response.data.length
                if(this.total < this.pageSize) {
                    this.show_list = this.total_list
                }else {
                    // 修改改数据之后显示回到第一页的bug，改为停留在当前页
                    let currentPage = this.$refs.page.currentPage
                    let _start = (currentPage-1) * this.pageSize
                    let _end = currentPage * this.pageSize
                    this.show_list = this.total_list.slice(_start,_end)
                }
            }
        }).catch(error =>{
                console.log(error)
        })

    }
}
</script>
<style scoped>

</style>